package com.ymanage.controller;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ymanage.entity.Custom;
import com.ymanage.service.CustomService;
@Controller
public class CustomController {
	@Autowired
	private CustomService customService;
	
	List<Custom> list = new ArrayList<Custom>();
	
	@RequestMapping("/custom.do")
	public String selectCustom(Custom custom,HttpServletRequest request){
		list=customService.selectAll(custom);
		request.setAttribute("Custom", list);
		return "custom.jsp";
	}
	@RequestMapping("/customUpdate.do")
	public String updateCustom(HttpServletRequest request,Integer cid){
		if(cid!=null){
			Custom custom = new Custom();
			custom.setCid(cid);
			list=customService.selectAll(custom);
			if(list.size()>0){
				custom=list.get(0);
				request.setAttribute("Custom", custom);
				request.getSession().setAttribute("cid",cid);
				return "customUpasAdd.jsp";
			}
		}
		return "redirect:custom.do";
	}
	@RequestMapping("/customAddasUpdate.do")
	public String updateasAdd(Custom custom,HttpSession session){
		if(session.getAttribute("cid")!=null){
			custom.setCid((Integer) session.getAttribute("cid"));
			session.removeAttribute("cid");
			customService.update(custom);
			return "redirect:custom.do";
		}else{
			customService.add(custom);
			return "redirect:custom.do";	
		}
	}
	@RequestMapping("/customDel.do")
	public String delCustom(Custom custom){
		customService.del(custom);
		return "redirect:custom.do";
	}
}
